package iplocation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

public class GetLoactionTask implements Runnable{
        LinkedBlockingQueue<JSONObject> queue = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    public GetLoactionTask(LinkedBlockingQueue<JSONObject> log) {
        this.queue=log;
    }

    public void run() {
            Connection conn  = JdbcUtil.getConnection();
            JSONObject o = null;
            try {
                while(null!=(o=queue.take())){
                   try{
                       String mobile = o.getString("mobile");
                       String name = o.getString("name");
                       String ip = o.getString("ip");
                       int userid = o.getInteger("userId");
                       if(ANYTASK.isCreator(userid)){
                           Map<String,Object> result = ANYTASK.getUserBaseInfo(userid);
                           HttpGet get = new HttpGet("http://api.map.baidu.com/location/ip?ak=coahrC7GYRZtYTcY5Y2bw8Gx7SUvvGF7&ip="+ip);
                           CloseableHttpResponse response = httpClient.execute(get);
                           String addr = EntityUtils.toString(response.getEntity());
                           response.close();
                           String location = JSON.parseObject(addr).getString("address");
                           PreparedStatement pstmt = conn.prepareStatement("replace into iplog (ip,name,mobile,location,charge_money,shop_count,trade_count,last_login_time,user_id) value (?,?,?,?,?,?,?,?,?)");
                           pstmt.setString(1,ip);
                           pstmt.setString(2,name);
                           pstmt.setString(3,mobile);
                           pstmt.setString(4,location);
                           if(!"[null]".equals(result.get("money")))pstmt.setInt(5, Integer.parseInt((String) result.get("money")) );else pstmt.setInt(5,0);
                           pstmt.setInt(6, Integer.parseInt((String) result.get("shopcount")) );
                           pstmt.setInt(7, (Integer) result.get("tradecount"));
                           pstmt.setString(8, (String) result.get("last_login_time"));
                           pstmt.setInt(9,userid);
                           pstmt.execute();
                           pstmt.close();
                       }
                   }catch (Exception e){
                        e.printStackTrace();
                   }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


     public boolean isCreator(int userId){
         //select * from team_member where status = 1 and team_member_id = 204
         return false;
     }

    public void getInfo(){

    }

}